package config

import (
	"io/ioutil"
	"log"
	"os"
	"time"

	"github.com/pelletier/go-toml"
)

// ConfigFile 服务配置对象定义
type ConfigFile struct {
	Common *CommonConfig `toml:"common"`
}

// CommonConfig 服务通用配置部分
type CommonConfig struct {
	ListenHost       string        `toml:"listen-host"`
	TCPPort          string        `toml:"tcp-port"`
	TCPCACert        string        `toml:"tcp-ca-cert"`
	TCPCert          string        `toml:"tcp-cert"`
	TCPKey           string        `toml:"tcp-key"`
	VerifyClientCert bool          `toml:"verify-client-cert"`
	ConnTimeout      time.Duration `toml:"conn-timeout"`
	ProcessorDir     string        `toml:"processor-dir"`
}

// ParseConfigFile 解析配置文件方法
func ParseConfigFile(filePath string, logger *log.Logger) *ConfigFile {
	var conf ConfigFile
	fs, err := ioutil.ReadFile(filePath)
	if err != nil {
		logger.Printf("config file with TOML format not found: %v", err)
		os.Exit(1)
	}
	if err := toml.Unmarshal(fs, &conf); err != nil {
		logger.Printf("config file with TOML format parse fail: %v", err)
		os.Exit(1)
	}
	return &conf
}
